package com.woniu.mq;

import com.rabbitmq.client.Channel;
import com.woniu.dao.PreOrderMapper;
import com.woniu.pojo.PreOrder;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;
@Component
public class PayConsumer {
    @Autowired
    private PreOrderMapper preOrderMapper;
    @RabbitListener(queues = {"dlq-queue"})
    @RabbitHandler
    public void pay(@Payload Map map, Channel channel, @Headers Map headers){
        String num=(String) map.get("num");
        PreOrder preOrder=preOrderMapper.findpay(num);
        if (preOrder.getStatus().equals("待支付")){
            preOrder.setStatus("失败");
            preOrderMapper.updateByPrimaryKey(preOrder);
        }
        Long id=(Long) headers.get(AmqpHeaders.DELIVERY_TAG);
        try {
            channel.basicAck(id,false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
